home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1994 June: Reference Library / Dev.CD Jun 94.toast / Periodicals / develop / develop Issue 15 / develop 15 code / Floating Windows / Sample / Source / eventHandler.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-04-22  |  8.1 KB  |  327 lines  |  [TEXT/MPS ]

  1. /*    MPW C Application skeleton by Dean Yu    */
  2. /*    Event handling routines                    */
  3.  
  4. #include <Types.h>
  5. #include <AppleEvents.h>
  6. #include <Desk.h>
  7. #include <Dialogs.h>
  8. #include <Editions.h>
  9. #include <EPPC.h>
  10. #include <Events.h>
  11. #include <Menus.h>
  12. #include <SegLoad.h>
  13. #include <TextUtils.h>
  14. #include <ToolUtils.h>
  15. #include <Windows.h>
  16.  
  17. #include "globals.h"
  18. #include "eventHandler.h"
  19. #include "menuDispatch.h"
  20. #include "WindowExtensions.h"
  21.  
  22. /*    Routine prototypes    */
  23.  
  24. void    DoMouseEvent (EventRecord *event);
  25. void    DoKeyEvent (EventRecord *event);
  26. void    DoActivateEvent(EventRecord *event);
  27. void    DoUpdateEvent (EventRecord *event);
  28. void    DrawWindowContent(Boolean doFill);
  29. void    DoOSEvent(EventRecord *event);
  30. void    DoHighLevelEvent(EventRecord *event);
  31. void    HandleGenericHighLevelEvent(EventRecord *event);
  32.     
  33. void    FatalErrorAlert(short    errID)
  34.  
  35. /*
  36.     Fatal error alert routine.
  37.     If an error occurs, this routine is called with the error message number
  38.     that message is pulled from the error STR# resource, and displayed in an
  39.     alert.  The application then quits when the user clicks the “OK” button.
  40. */
  41.  
  42. {
  43.     short    alrtItem;
  44.     Str255    errString;
  45.     
  46.     GetIndString(&errString, rFatalErrorStringsID, errID);
  47.     ParamText(errString, nil, nil, nil);
  48.     alrtItem = Alert(rFatalErrorAlertID, nil);
  49.     ExitToShell();
  50. }
  51.  
  52. void    EventLoop()
  53. {
  54.     EventRecord    event;
  55.     DialogPtr    theDialog;
  56.     short        itemHit;
  57.     Boolean        gotEvent;
  58.     
  59.     gDone = false;
  60.     gInBackground = false;
  61.     
  62.     do {
  63.         gotEvent = WaitNextEvent(everyEvent, &event, 15, nil);
  64.         if (gotEvent) {
  65.             if (!IsDialogEvent(&event))
  66.                 HandleEvent(&event);
  67.             else {
  68.                 DialogSelect(&event, &theDialog, &itemHit);
  69.                 if ((itemHit == ok) || (itemHit == cancel))
  70.                     DisposeFindDialog();
  71.                 
  72.                 if (event.what == osEvt)
  73.                     DoOSEvent(&event);
  74.             }
  75.         }
  76.         
  77.         // If we’re in the background, and the find dialog is up, close it after
  78.         // a time to demonstrate what should happen to floating windows in the background.
  79.         
  80.         if ((gInBackground != false) && (gTimeToDialogDismissal > 0)) {
  81.             --gTimeToDialogDismissal;
  82.             if (gTimeToDialogDismissal == 0)
  83.                 DisposeFindDialog();
  84.         }
  85.     } while (!gDone);
  86.     ExitToShell();
  87. }
  88.  
  89. void    HandleEvent(EventRecord    *event)
  90. {
  91.     switch (event->what) {
  92.         case mouseDown:            DoMouseEvent(event);
  93.                                 break;
  94.         case keyDown:
  95.         case autoKey:            DoKeyEvent(event);
  96.                                 break;
  97.         case activateEvt:        DoActivateEvent(event);
  98.                                 break;
  99.         case updateEvt:            DoUpdateEvent(event);
  100.                                 break;
  101.         case osEvt:                DoOSEvent(event);
  102.                                 break;
  103.         case kHighLevelEvent:    DoHighLevelEvent(event);
  104.                                 break;
  105.                                                         
  106.     }
  107. }
  108.  
  109. void    DoMouseEvent(EventRecord *event)
  110. {
  111.     short        part;
  112.     long        growResult;
  113.     WindowRef    whichWindow;
  114.     WindowRef    frontWindow;
  115.     
  116.     part = FindWindow(event->where, &((WindowPtr) whichWindow));
  117.     switch (part) {
  118.         case inDesk:        break;
  119.         case inMenuBar:     DoMenuCommand(MenuSelect(event->where));
  120.                             break;
  121.         case inSysWindow:    SystemClick(event, (WindowPtr) whichWindow);
  122.                             break;
  123.         case inContent:        frontWindow = (WindowRef) FrontWindow();
  124.                             if ((GetWindowKind(frontWindow) == dialogKind) &&
  125.                                 (whichWindow != frontWindow))
  126.                                     SysBeep(1);
  127.                             else
  128.                                 if (whichWindow != frontWindow)
  129.                                     SelectReferencedWindow(whichWindow);
  130.                             break;
  131.         case inDrag:        frontWindow = (WindowRef) FrontWindow();
  132.                             if ((GetWindowKind(frontWindow) == dialogKind) &&
  133.                                 (whichWindow != frontWindow))
  134.                                     SysBeep(1);
  135.                             else
  136.                                 DragReferencedWindow(whichWindow, event->where, &gDragArea);
  137.                             break;
  138.         case inGrow:        growResult = GrowWindow((WindowPtr) whichWindow, event->where, &gGrowBounds);
  139.                                 SizeWindow((WindowPtr) whichWindow, growResult & 0x0000FFFF, (growResult & 0xFFFF0000) >> 16, true);
  140.                             break;
  141.         case inGoAway:        if (TrackGoAway((WindowPtr) whichWindow, event->where)) {
  142.                                 DisposeWindowReference(whichWindow);
  143.                                 if (FrontNonFloatingWindow() == nil)
  144.                                     DisableItem(GetMHandle(rFileMenuID), kCloseItem);
  145.                             }
  146.                             break;
  147.         case inZoomIn:
  148.         case inZoomOut:        break;
  149.     }
  150. }
  151.  
  152. void    DoKeyEvent(EventRecord *event)
  153. {
  154.     long    theKey;
  155.     
  156.     theKey = event->message & charCodeMask;
  157.     if ((event->modifiers & cmdKey) && (event->what == keyDown))
  158.         DoMenuCommand(MenuKey((char) theKey));
  159. }
  160.  
  161. void DoActivateEvent(EventRecord *event)
  162. {
  163.     Boolean    activating = false;
  164.     
  165.     if (GetWindowKind((WindowRef) event->message) != dialogKind) {
  166.         if (event->modifiers & activeFlag)
  167.             activating = true;
  168.         
  169.         DebugStr("\pGot activate from event loop.");
  170.         
  171.         ActivateEventHandler((WindowRef) event->message, activating);
  172.     }
  173. }
  174.  
  175. pascal void ActivateEventHandler(WindowRef theWindow, Boolean activateWindow)
  176. {
  177.     GrafPtr    currentPort;
  178.     
  179.     GetPort(¤tPort);
  180.     SetPort((GrafPtr) theWindow);
  181.     
  182.     DrawWindowContent(activateWindow);
  183.     
  184.     if (activateWindow == false)
  185.         SetPort(currentPort);
  186. }
  187.  
  188. pascal void    RecordingFloaterActivateHandler(WindowRef theWindow, Boolean activateWindow)
  189. {
  190.     GrafPtr        currentPort;
  191.     PicHandle    recordingControlsPicture;
  192.     short        whichPicture;
  193.     
  194.     GetPort(¤tPort);
  195.     SetPort((GrafPtr) theWindow);
  196.     
  197.     if (activateWindow == true)
  198.         whichPicture = 128;
  199.     else
  200.         whichPicture = 129;
  201.     recordingControlsPicture = GetPicture(whichPicture);
  202.     
  203.     DrawPicture(recordingControlsPicture, &((**recordingControlsPicture).picFrame));
  204.     SetWindowPic((WindowPtr) theWindow, recordingControlsPicture);
  205.     
  206.     if (activateWindow == false)
  207.         SetPort(currentPort);
  208. }
  209.  
  210. pascal void    ToolsFloaterActivateHandler(WindowRef theWindow, Boolean activateWindow)
  211. {
  212.     GrafPtr        currentPort;
  213.     PicHandle    toolsPicture;
  214.     short        whichPicture;
  215.     
  216.     GetPort(¤tPort);
  217.     SetPort((GrafPtr) theWindow);
  218.     
  219.     if (activateWindow == true)
  220.         whichPicture = 130;
  221.     else
  222.         whichPicture = 131;
  223.     toolsPicture = GetPicture(whichPicture);
  224.     
  225.     DrawPicture(toolsPicture, &((**toolsPicture).picFrame));
  226.     SetWindowPic((WindowPtr) theWindow, toolsPicture);
  227.     
  228.     if (activateWindow == false)
  229.         SetPort(currentPort);
  230. }
  231.  
  232. void    DoUpdateEvent(EventRecord *event)
  233. {
  234.     WindowPtr    theWindow = (WindowPtr) event->message;
  235.     
  236.     SetPort(theWindow);
  237.     BeginUpdate(theWindow);
  238.         EraseRect(&(theWindow->portRect));
  239.         DrawWindowContent(theWindow == FrontNonFloatingWindow());
  240.     EndUpdate(theWindow);
  241. }
  242.  
  243. void    DrawWindowContent(Boolean doFill)
  244. {
  245.     Rect    tempRect;
  246.  
  247.     SetRect(&tempRect, 5, 5, 50, 50);
  248.     
  249.     if (doFill == true)
  250.         FillRect(&tempRect, &(qd.black));
  251.     else {
  252.         FrameRect(&tempRect);
  253.         InsetRect(&tempRect, 1, 1);
  254.         EraseRect(&tempRect);
  255.     }
  256. }
  257.  
  258. void    DoOSEvent(EventRecord *event)
  259. {
  260.     switch ((event->message >> 24) & 0xFF) {
  261.         case mouseMovedMessage:        break;
  262.         case suspendResumeMessage:    if (event->message & resumeFlag) {
  263.                                         gInBackground = false;
  264.                                         gTimeToDialogDismissal = 0;
  265.                                         DisableItem(GetMHandle(rEditMenuID), 0);
  266.                                         DrawMenuBar();
  267.                                         ResumeFloatingWindows();
  268.                                         SetCursor(&qd.arrow);
  269.                                     }
  270.                                     else {
  271.                                         gInBackground = true;
  272.                                         if (GetWindowKind((WindowRef) FrontWindow()) == dialogKind)
  273.                                             gTimeToDialogDismissal = 5;
  274.                                         SuspendFloatingWindows();
  275.                                     }
  276.                                     break;
  277.     }
  278. }
  279.  
  280.  
  281. /*    Handle high level events */
  282.  
  283. void    DoHighLevelEvent(EventRecord *event)
  284. {
  285.     OSErr    AEProcessResult;
  286.     
  287.     switch    (event->message) {
  288.         case kCoreEventClass:    AEProcessResult = AEProcessAppleEvent(event);    /*    Handle core AppleEvents */
  289.                                 break;
  290.         case rSectionType:        break;                                            /*    Handle Edition Manager AppleEvents */
  291.         default:                HandleGenericHighLevelEvent(event);                /*    Other high level events */
  292.                                 break;
  293.     }
  294. }
  295.  
  296. void    HandleGenericHighLevelEvent(EventRecord *event)
  297. {
  298.     TargetID    eventSenderInfo;
  299.     unsigned    long eventIdentifier;
  300.     Ptr            dataBuffer;
  301.     unsigned    long dataSize;
  302.     OSErr    HLEventError;
  303.     
  304.     dataSize = 0;
  305.     dataBuffer = nil;
  306.     HLEventError = AcceptHighLevelEvent(&eventSenderInfo, &eventIdentifier, dataBuffer, &dataSize);
  307.     
  308.     /*
  309.         Since we don’t know how much data is coming in, call AcceptHighLevelEvent once to
  310.         find out how much data was sent, then use create a new buffer large enough to hold
  311.         all the data, then call AcceptHighLevelEvent again to get the data.
  312.     */
  313.     
  314.     if (HLEventError == bufferIsSmall) {
  315.         dataBuffer = NewPtr(dataSize);
  316.         HLEventError = AcceptHighLevelEvent(&eventSenderInfo, &eventIdentifier, dataBuffer, &dataSize);
  317.     }
  318.         
  319.     /*    Dispatch the high level event to the proper routine depending on the message class */
  320.     
  321.     switch    (event->message) {
  322.     }
  323.     
  324.     if (dataBuffer)
  325.         DisposPtr(dataBuffer);
  326. }
  327.